<script setup>

import {ref, onMounted, watch, computed} from 'vue';
import axios from "axios";
import { message } from 'ant-design-vue';

// 查询
const pageSize = ref(10);
const pageNo = ref(1)
const items = ref([]);
const loading = ref(true);
const totalItems = ref(0);
const pagination = computed(() => ({
    total: totalItems.value,
    current: pageNo.value,
    pageSize: pageSize.value
})) 
const columns = ref([
    {
        title: "ID",
        dataIndex: "logId",
        key: 'logId'
    },
    {
        title: "用户",
        dataIndex: "username",
        key: 'username'
    },
    {
        title: "事件",
        dataIndex: "logText"
    },
    {
        title: "时间",
        dataIndex: "createTime",
    }
])
const open = ref(false);
const title = ref("");
const formData = ref({});

function loadItems({page, itemsPerPage}) {
    loading.value = true;
    pageNo.value = page
    pageSize.value = itemsPerPage
    axios.post('/Log/getLogList', {
        page: page,
        pageSize: itemsPerPage,
        userId: JSON.parse(localStorage.getItem("ZAYY_USER_INFO")).userId
    }).then(({data}) => {
        items.value = data.data.records;
        totalItems.value = data.data.total;
        loading.value = false;
    })
}

onMounted(() => {
    loadItems({page: 1, itemsPerPage: pageSize.value});
});

const dialogEnum = {
    update: "编辑",
    create: "新增"
}

function openData(data) {
    open.value = true
    title.value = dialogEnum.update
    formData.value = data
}


function deleteRole(item, type) {
    axios.post("/User/disableUser", {userId: item.userId, status: type}).then(res => {
        if(res.data.code == 0) {
            message.success('操作成功！')
            loadItems({page: 1, itemsPerPage: pageSize.value});
        } else {
            message.error(res.data.message)
        }
    }, err => {
        message.error(err.data.message)
    })
}

function handleTabChange(pag){
    loadItems({page: pag.current, itemsPerPage: pag.pageSize})
}
</script>

<template>
    <a-spin size="large" :spinning="loading">
        <a-card title="日志管理">
            <a-table :dataSource="items" :columns="columns" :pagination="pagination" @change="handleTabChange"></a-table>
        </a-card>
    </a-spin>
</template>

<style scoped lang="less">

</style>
7h